package com.yuhanginfo.modules.web.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.entity.ShoppingCart;
import com.yuhanginfo.modules.web.entity.WebCarBrand;
import com.yuhanginfo.modules.web.entity.WebYongUser;
import com.yuhanginfo.modules.web.service.WebCarBrandService;
import com.yuhanginfo.modules.web.service.WebCarouselService;
import com.yuhanginfo.modules.web.service.WebClassService;
import com.yuhanginfo.modules.web.service.WebGoodsComboService;
import com.yuhanginfo.modules.web.service.WebYongUserService;

/**
 * 首页
 * @author yinhao
 * @version 2017-10-30
 */
@Controller
@RequestMapping(value = "${frontPath}/webIndex")
public class WebIndexController extends BaseController {

	@Autowired
	private WebYongUserService webYongUserService;
	@Autowired
	private WebClassService webClassService;
	@Autowired
	private WebCarBrandService carBrandService;
	@Autowired
	private WebGoodsComboService webGoodsComboService;
	@Autowired
	private WebCarouselService webCarouselService;
	
	@ModelAttribute
	public WebYongUser get(@RequestParam(required=false) String id) {
		WebYongUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = webYongUserService.get(id);
		}
		if (entity == null){
			entity = new WebYongUser();
		}
		return entity;
	}

	@RequestMapping(value = "")
	public String index(ShoppingCart shoppingCart, Model model) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		String userId;
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		model.addAttribute("allCarousels",webCarouselService.getAllCarousel());
		if("1".equals(yongUser.getUserType())){
			userId = null;
		}else{
			userId = yongUser.getId();
		}
		model.addAttribute("allGoodsComboList",webGoodsComboService.selectByUserId(userId));
		List<WebCarBrand> carBrand = carBrandService.findList(new WebCarBrand());
		model.addAttribute("carBrand",carBrand);
		return "modules/web/webIndex";
	}

}